<?php

namespace app\admin\controller;

use app\api\model\ConfigInvite;
use app\api\model\Dymanic;
use app\api\model\Privatechat;
use app\api\model\User;
use app\api\model\Task;
use app\api\model\UserImg as UserImgModel;
use think\Db;

class Verify extends Common
{
    public function index()
    {
        $where = [];

        $types = input('types', 1);
        if ($types) {
            $where['types'] = $types;
        }

        $extra = input('extra/d', 0);
        if ($extra) {
            $where['extra'] = $extra;
        }
        $this->assign('extra', $extra);

        $status = input('status') ?: 0;
        $where['status'] = $status;


        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where['t2.' . $search_type] = ['like', '%' . $search_name . '%'];
        }
        $entertainments = ['娱乐','分享','交友','相亲'];
        $data = Db::name('user_material_verify t1')
            ->master()
            ->field('t1.*, t2.name,t2.phone,t2.sex,t2.entertainment,t2.age')
            ->join('user t2', 't2.id = t1.user_id')
            ->where($where)
            ->order('t1.create_time desc')
            ->paginate(_getCachePages(), false, ['query' => request()->param()])
            ->each(function($item) use ($entertainments)
            {
                // if ($item['types'] == 1 && $item['extra']) {
                    $item['authpic'] = Db::name('auth')->where(['user_id'=>$item['user_id']])->value('image1');
                // }
                $item['entertainment_text'] = $entertainments[$item['entertainment']];
                return $item;
            });

        $this->assign('data', $data);

        $this->assign('types', $types);
        $this->assign('status', $status);

        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function update()
    {
        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');

            if ($id) {
                $verify = Db::name('user_material_verify')->find($id);

                $db = Db::name('user_material_verify')->where('id', $id)->update(['status' => $post['status'], 'remark' => $post['remark'], 'update_time' => time(), 'shenhe_time'=>time(), 'shenhe_admin_id'=>$this->adminInfo['id']]);

                switch ($verify['types']) {
                    case 1:
                        if ($post['status'] == 1) {
                            $thumb_ = explode('.', $verify['content']);
                            $thumb = $thumb_[0] . '_thumb.' . $thumb_[1];
                            $noHttp = strpos($verify['content'], 'https://') === false;
                            $contain = [
                                'id' => $verify['user_id'],
                                'head_100' => $noHttp ? $thumb : $verify['content'],
                                'head_300' => $noHttp ? $thumb : $verify['content'],
                                'head_portrait' => $verify['content'],
                                'real_avatar'   => $verify['extra']
                            ];

                            $user = User::get($verify['user_id']);

                            if ($verify['extra']) {
                                if ($user->quality == 3) {
                                    $contain['quality'] = 4;
                                }
                                Task::finish('real_avatar', $verify['user_id']);
                            }
                            User::update($contain);

                            UserImgModel::where(['types' => 1, 'user_id' => $verify['user_id']])->setField('is_avatar', 0);
                            UserImgModel::create([
                                'user_id' => $verify['user_id'],
                                'pic' => json_encode([$verify['content']], JSON_UNESCAPED_SLASHES),
                                'picthumb' => json_encode([$thumb], JSON_UNESCAPED_SLASHES),
                                'is_avatar' => 1,
                                'is_face'   => $verify['extra']
                            ]);
                            (new \app\service\TencentIM)->account_import($user['id'], $user['name'], $verify['content']);
                        } else {
                            $content = $post['remark'] ? '您上传的头像被驳回,驳回原因：'.$post['remark'] : '上传的头像不符合国家法律法规，请重新上传';
                            // Privatechat::notice($verify['user_id'], $content);
                        }
                        break;
                    case 2:
                        if ($post['status'] == 1) {
                            $contain = [
                                'id' => $verify['user_id'],
                                'wxcount' => $verify['content'],
                                'wx_qrcode' => $verify['extra'],
                                'link_auth' => 1,
                                'link_auth_time' => time(),
                            ];

                            User::update($contain);
                        } else {
                            $contain = [
                                'id' => $verify['user_id'],
                                'wxcount' => '',
                                'wx_qrcode' => '',
                                'link_auth' => 0,
                                'link_auth_time' => time(),
                            ];

                            User::update($contain);

                            $content = $post['remark'] ? '您的社交档案被驳回,驳回原因：'.$post['remark'] : '社交档案不符合国家法律法规，请重新修改';
                            // Privatechat::notice($verify['user_id'], $content);
                        }
                        break;
                    case 3:
                        if ($post['status'] == 1) {
                            // $contain = [
                            //     'id' => $verify['user_id'],
                            //     'name' => $verify['content'],
                            // ];

                            // User::update($contain);
                        } else {
                            $content = $post['remark'] ? '您的昵称被驳回,驳回原因：'.$post['remark'] : '您的昵称违规，请重新修改';
                            $contain = [
                                'id' => $verify['user_id'],
                                //'name' => '违规昵称' . uniqid('_'),
                                'name' => '用户' . $verify['user_id'],
                            ];

                            User::update($contain);
                            Privatechat::notice($verify['user_id'], $content);
                        }
                        break;
                    case 4:
                        if ($post['status'] == 1) {
                            $contain = [
                                'id' => $verify['user_id'],
                                'slogan' => $verify['content'],
                            ];

                            User::update($contain);
                        } else {
                            $content = $post['remark'] ? '您的自我介绍被驳回,驳回原因：'.$post['remark'] : '自我介绍不符合国家法律法规，请重新修改';
                            // Privatechat::notice($verify['user_id'], $content);
                            $contain = [
                                'id' => $verify['user_id'],
                                'slogan' => '',
                            ];
                            User::update($contain);
                        }
                        break;
                    case 6:
                        if ($post['status'] == 1) {
                            $contain = [
                                'id' => $verify['user_id'],
                                'self_slogan' => $verify['content'],
                            ];

                            User::update($contain);
                        } else {
                            $content = $post['remark'] ? '您自我介绍被驳回,驳回原因：'.$post['remark'] : '自我介绍不符合国家法律法规，请重新修改';
                            // Privatechat::notice($verify['user_id'], $content);
                            $contain = [
                                'id' => $verify['user_id'],
                                'self_slogan' => '',
                            ];
                            User::update($contain);
                        }
                        break;
                    case 5:
                        if ($post['status'] == 1) {
                            $datas = json_decode($verify['content'], true);
                            $image = explode(',', $datas['pic']);
                            $cover = explode(',', $datas['cover']);

                            $contain = [];
                            foreach ($image as $key => $item) {
                                $contain[] = [
                                    'types' => $datas['types'],
                                    'user_id' => $verify['user_id'],
                                    'pic' => stripslashes(json_encode([$item])),
                                    'picthumb' => stripslashes(json_encode([$item])),
                                    'burn' => $datas['burn'] ?? 0,
                                    'packet' => $datas['packet'],
                                    'cover' => $cover[$key] ?? '',
                                    'status' => 1
                                ];
                            }

                            (new UserImgModel)->saveAll($contain);

                            $user = Db::name('user')->field('id, city, longitude, latitude')->where(['id' => $verify['user_id']])->find();
                            $contain = [
                                'is_photos' => 1,
                                'userid' => $verify['user_id'],
                                'citys' => $user['city'],
                                'content' => '我刚更新了相册，快来看吧',
                                'longitude' => $user['longitude'],
                                'latitude' => $user['latitude'],
                                'shenhe' => 1,
                            ];

                            if ($datas['types'] == 1) {
                                $contain['sumbImage'] = implode(',', $image);
                                $contain['masterimg'] = implode(',', $image);
                            } else {
                                $contain['video'] = implode(',', $image);
                                $contain['cover'] = implode(',', $cover);
                            }

                            (new Dymanic)->create($contain);

                            ConfigInvite::reward($verify['user_id'], 'invite_verify');
                        } else {
                            $content = $post['remark'] ? '您上传的照片被驳回,驳回原因：'.$post['remark'] : '照片不符合国家法律法规，请重新上传';
                            // Privatechat::notice($verify['user_id'], $content);
                        }
                        break;
                    case 7:
                        if ($post['status'] == 1) {
                        } else {
                            $content = $post['remark'] ? '您上传的用户中心背景图被驳回,驳回原因：'.$post['remark'] : '您的用户详情背景图违规，请重新上传';
                            $contain = [
                                'id' => $verify['user_id'],
                                'cover' => '',
                            ];

                            User::update($contain);
                            Privatechat::notice($verify['user_id'], $content);
                        }
                        break;
                }


                if ($db) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            } else {
                $data = ["err" => '参数缺失'];
            }
        }
        return $data;
    }


    public function shenheAll()
    {
        if (request()->isAjax()) {
            $post = input('post.');
            $status = input('status');
            $time = time();
            foreach ($_POST['delAll'] as $v) {
                $verify = Db::name('user_material_verify')->find($v);
                $db = Db::name('user_material_verify')->where('id', $v)->update(['status' => $status, 'update_time' => $time, 'shenhe_time'=>time(), 'shenhe_admin_id'=>$this->adminInfo['id']]);
                switch ((int)$verify['types']) {
                    case 1:
                        if ($status == 1) {
                            $thumb_ = explode('.', $verify['content']);
                            $thumb = $thumb_[0] . '_thumb.' . $thumb_[1];
                            $noHttp = strpos($verify['content'], 'https://') === false;
                            $contain = [
                                'id' => $verify['user_id'],
                                'head_100' => $noHttp ? $thumb : $verify['content'],
                                'head_300' => $noHttp ? $thumb : $verify['content'],
                                'head_portrait' => $verify['content'],
                            ];
                            $user = User::get($verify['user_id']);
                            
                            if ($verify['extra']) {
                                $contain['real_avatar'] = 1;
                                if ($user->quality == 3) {
                                    $contain['quality'] = 4;
                                }
                                Task::finish('real_avatar', $verify['user_id']);
                            }
                            User::update($contain);
                        }
                    break;
                    case 3:
                        if ($status == 1) {
                            // $contain = [
                            //     'id' => $verify['user_id'],
                            //     'name' => $verify['content'],
                            // ];
                            // User::update($contain);
                        } else {
                            $content = '您的昵称违规，请重新修改';
                            $contain = [
                                'id' => $verify['user_id'],
                                'name' => '违规昵称' . uniqid('_'),
                            ];

                            User::update($contain);
                            Privatechat::notice($verify['user_id'], $content);
                        }
                    break;
                    case 4:
                        if ($status == 1) {
                            $contain = [
                                'id' => $verify['user_id'],
                                'slogan' => $verify['content'],
                            ];
                            User::update($contain);
                        } else {
                            $content = '您的自我介绍违规，请重新修改';
                            Privatechat::notice($verify['user_id'], $content);
                            $contain = [
                                'id' => $verify['user_id'],
                                'slogan' => '',
                            ];
                            User::update($contain);
                        }
                    break;
                    case 6:
                        if ($status == 1) {
                            $contain = [
                                'id' => $verify['user_id'],
                                'self_slogan' => $verify['content'],
                            ];
                            User::update($contain);
                        } else {
                            $content = '您的自我介绍违规，请重新修改';
                            Privatechat::notice($verify['user_id'], $content);
                            $contain = [
                                'id' => $verify['user_id'],
                                'self_slogan' => '',
                            ];
                            User::update($contain);
                        }
                    break;
                }
            }
            $data = ['ret' => '更新成功'];
        } else {
            $data = ['err' => '提交类型错误'];
        }
        return $data;
    }

    public function watch()
    {
        if (!input('?id')) {
            $this->error("参数错误");
        }
        $id = input('id/d');

        $data = Db::name('user_material_verify')->find($id);

        $content = [];
        if ($data['content']) {
            $content = json_decode($data['content'], true);

            $content['images'] = explode(',', $content['pic']);
        }

        $this->assign('content', $content);

        return $this->fetch();
    }

    public function del()
    {
        if (request()->isAjax()) {

            $id = input('id');
            if ($id) {
                $verify = Db::name('user_material_verify')->find($id);

                if ($verify['types'] == 2) {
                    $contain = [
                        'id' => $verify['user_id'],
                        'wxcount' => '',
                        'wx_qrcode' => '',
                        'link_auth' => 0,
                        'link_auth_time' => time(),
                    ];

                    User::update($contain);
                } else if ($verify['types'] == 1) {
                    $user = User::get($verify['user_id']);
                    $sex = $user && $user['sex'] == '女' ? 'female' : 'male';
                    $head = '/public/style/api/img/'.$sex.'/' . mt_rand(1,20) . '.jpg';

                    $contain = [
                        'id' => $verify['user_id'],
                        'head_100' => $head,
                        'head_300' => $head,
                        'head_portrait' => $head,
                        'real_avatar'   => 0
                    ];

                    if (strpos($verify['content'], 'wx.qlogo.cn/') == false) {
                        try {
                            \app\service\AliOss::delete($verify['content']);
                        } catch (\Exception $e) {
                            
                        }
                    }
                    UserImgModel::destroy([
                        'user_id' => $verify['user_id'],
                        'pic' => json_encode([$verify['content']], JSON_UNESCAPED_SLASHES),
                        'is_avatar' => 1
                    ]);

                    User::update($contain);
                }

                $db = Db::name('user_material_verify')->delete($id);

                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }

    public function goddess()
    {
        $where = [];

        $type = input('type') ?: 1;
        if ($type) {
            $where['type'] = $type;
        }

        $status = input('status') ?: 0;

        $where['t1.status'] = $status;

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where['t2.' . $search_type] = ['like', '%' . $search_name . '%'];
        }

        $data = Db::name('authentication_img t1')
            ->field('t1.*, t2.name,t2.phone')
            ->join('user t2', 't2.id = t1.user_id')
            ->where($where)
            ->order('t1.id desc')
            ->paginate(_getCachePages(), false, ['query' => request()->param()]);


        $this->assign('type', $type);
        $this->assign('status', $status);
        $this->assign('data', $data);

        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function updateGoddess()
    {
        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');

            if ($id) {
                $auth = Db::name('authentication_img')->find($id);

                $db = Db::name('authentication_img')->where('id', $id)->update(['status' => $post['status'], 'update_time' => time()]);

                if ($auth['type'] == 1) {
                    if ($post['status'] == 1) {
                        Db::name('user_img')->insert([
                            'user_id' => $auth['user_id'],
                            'pic' => json_encode([$auth['pic']]),
                            'picthumb' => json_encode([$auth['pic']]),
                            'burn' => 0,
                            'packet' => 0,
                            'is_auth' => 1
                        ]);

                        $contain = ['attestation' => 1, 'is_identity_authentication' => 2];
                    } else {
                        $contain = ['attestation' => 0, 'is_identity_authentication' => 3];
                    }
                } else {
                    if ($post['status'] == 1) {
                        $contain = ['nvshen' => 1];
                        $content = '女神认证审核已通过';
                    } else {
                        $contain = ['nvshen' => 0];
                        $content = '女神认证审核未通过，请重新上传本人视频进行认证';
                    }

                    // Privatechat::notice($auth['user_id'], $content);
                }

                if ($db) {
                    Db::name('user')->where(['id' => $auth['user_id']])->update($contain);

                    if ($post['status'] == 1) {
                        ConfigInvite::reward($auth['user_id'], 'invite_verify');
                    }

                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            } else {
                $data = ["err" => '参数缺失'];
            }
        }
        return $data;
    }

    public function delGoddess()
    {
        if (request()->isAjax()) {

            $id = input('id');
            if ($id) {
                $auth = Db::name('authentication_img')->find($id);

                if ($auth) {
                    Db::name('user')->where(['id' => $auth['user_id']])->update(['nvshen' => 0]);
                }

                $db = Db::name('authentication_img')->delete($id);

                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }




    public function setRealFace($id, $state)
    {
        $result = Db::name('user_material_verify')->where(['id'=>$id])->update(['extra'=>$state]);

        if ($result) {
            $data = ['ret' => '设置成功'];
        } else {
            $data = ['err' => '设置失败'];
        }
        return $data;
    }
}
